package view;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Observable;
import java.util.Observer;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import mode.Chess;
import mode.ChessArray;
import util.SocketUtil;
import util.StateCode;

public class GridBagPanel extends JPanel implements Observer {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private int chessw, chessh, left, top;
	private int chessX, chessY;
	private ChessArray chessArray;
	private boolean isPaintMan = false;
	private boolean isSavePanit = false;
	private boolean isChess=false;
	private Chess[][] chessarray;
	private Integer isMouseListener = StateCode.INITLISTENER;
	private int selectValue;
	private boolean win=false;

	public GridBagPanel(ChessArray chessArray) {
		// TODO Auto-generated constructor stub
		this.chessArray = chessArray;
		setBackground(new Color(0x87CEEB));
		setOpaque(false);

		addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				super.mouseClicked(e);
				switch (isMouseListener) {
				case StateCode.ISMOUSELISTENER:
					isPaintMan = true;
					Chess chess = new Chess();
					int x = e.getX() * 12 / getWidth(), y = e.getY() * 12 / getHeight();
					chessX = (int) (x * getWidth() / 12) + 12;
					chessY = (int) (y * getHeight() / 12) + 10;

					//判断是否已经有棋子
					if (isChess) {
						for (int i = 0; i < chessarray.length; i++) 
						for (int j = 0; j < chessarray[i].length; j++) 
							if (chessarray[i][j] != null) 
						    if (x == j && y == i) {JOptionPane.showMessageDialog(GridBagPanel.this, "该地方已有棋子，请在空白地方落子！");return;}
					}

					chess.setX(x);
					chess.setY(y);
					chess.setColor(getSelectValue());

					repaint();

					chessArray.saveMyChess(chess);
					SocketUtil.sendMessage(StateCode.ISMOUSELISTENER);
					SocketUtil.sendMessage(chess);

					if (getSelectValue() == StateCode.Black)
						SocketUtil.sendMessage(StateCode.Black);
					if (getSelectValue() == StateCode.WHITE)
						SocketUtil.sendMessage(StateCode.WHITE);
					isMouseListener = StateCode.WAITOPPOSITECHESS;
					break;
				case StateCode.WAITBANKERCHESS:
					JOptionPane.showMessageDialog(GridBagPanel.this, "对方为庄，请等待对方先下！");
					break;
				case StateCode.WAITSELECTBANKER:
					JOptionPane.showMessageDialog(GridBagPanel.this, "请等待对方选庄！");
					break;
				case StateCode.WAITOPPOSITECHESS:
					JOptionPane.showMessageDialog(GridBagPanel.this, "请等待对方落子！");
					break;
				case StateCode.ISBANKER:
					JOptionPane.showMessageDialog(GridBagPanel.this, "请先选庄！");
					break;
				case StateCode.SELECTCOLOR:
					Object[] color = { "黑色", "白色" };
					Object selectValue = JOptionPane.showInputDialog(GridBagPanel.this, "请选择棋子的颜色", "提示信息",
							JOptionPane.INFORMATION_MESSAGE, null, color, color[0]);
					if (selectValue.toString().equals("黑色"))
						setSelectValue(StateCode.Black);
					if (selectValue.toString().equals("白色"))
						setSelectValue(StateCode.WHITE);
					isMouseListener = StateCode.ISMOUSELISTENER;
					break;
				case StateCode.INITLISTENER:
					break;
				}
			}
		});
	}

	@Override
	public void paint(Graphics g) {
		// TODO Auto-generated method stub
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D) g;
		Composite composite = g2.getComposite();
		g2.setColor(new Color(0x87CEEB));
		g2.setComposite(AlphaComposite.SrcOver.derive(0.6f));
		g2.fillRect(0, 0, getWidth(), getHeight());
		g2.setComposite(composite);
		drawGrid(g2);
		drawChess(g2);
		if (isSavePanit)
			drawSaveChess(g2);
		isPaintMan = false;
		isSavePanit = false;
	}

	private void drawGrid(Graphics2D g2) {
		int width = getWidth();
		int height = getHeight();
		chessw = width / 12;
		chessh = height / 12;
		left = width % 12 / 2;
		top = height % 12 / 2;
		int x = left + chessw / 2, y = top + chessh / 2;
		for (int i = 0; i < 13; i++) {
			g2.setColor(Color.BLACK);
			g2.drawLine(x + i * chessw, y, x + i * chessw, height - y);
			g2.drawLine(x, y + i * chessh, width - x, y + i * chessh);
		}
	}

	private void drawChess(Graphics2D g2) {
		if (isPaintMan) {
			Composite composite = g2.getComposite();
			if (getSelectValue() == StateCode.Black) {
				Image image = new ImageIcon(getClass().getResource("../image/blackChessman.jpg")).getImage();
				g2.drawImage(image, chessX, chessY, null);
				g2.setComposite(composite);
			}
			if (getSelectValue() == StateCode.WHITE) {
				g2.setComposite(AlphaComposite.SrcOver.derive(0.9f));
				Image image = new ImageIcon(getClass().getResource("../image/whiteChessman.jpg")).getImage();
				g2.drawImage(image, chessX, chessY, null);
				g2.setComposite(composite);
			}
		}
	}

	private void drawSaveChess(Graphics2D g2) {
		for (int i = 0; i < chessarray.length; i++) {
			for (int j = 0; j < chessarray[i].length; j++) {
				if (chessarray[i][j] != null) {
					Composite composite = g2.getComposite();
					if (chessarray[i][j].getColor() == StateCode.Black) {
						Image image = new ImageIcon(getClass().getResource("../image/blackChessman.jpg")).getImage();
						g2.drawImage(image, j * getWidth() / 12 + 12, i * getHeight() / 12 + 10, null);
						g2.setComposite(composite);
					}
					if (chessarray[i][j].getColor() == StateCode.WHITE) {
						Image image = new ImageIcon(getClass().getResource("../image/whiteChessman.jpg")).getImage();
						g2.drawImage(image, j * getWidth() / 12 + 12, i * getHeight() / 12 + 10, null);
						g2.setComposite(composite);
					}
				}
			}
		}
	}

	@Override
	public void update(Observable o, Object arg) {
		// TODO Auto-generated method stub
		chessarray = (Chess[][]) arg;
		isSavePanit = true;
		repaint();
	}
    
	public boolean isSavePanit() {
		return isSavePanit;
	}

	public void setSavePanit(boolean isSavePanit) {
		this.isSavePanit = isSavePanit;
	}

	public Chess[][] getChessarray() {
		return chessarray;
	}

	public void setChessarray(Chess[][] chessarray) {
		this.chessarray = chessarray;
	}

	public int getSelectValue() {
		return selectValue;
	}

	public void setSelectValue(int selectValue) {
		this.selectValue = selectValue;
	}

	public Integer getIsMouseListener() {
		return isMouseListener;
	}

	public void setIsMouseListener(Object arg) {
		this.isMouseListener = (Integer) arg;
	}

	public boolean isChess() {
		return isChess;
	}

	public void setChess(boolean isChess) {
		this.isChess = isChess;
	}

	public boolean isWin() {
		return win;
	}

	public void setWin(boolean win) {
		this.win = win;
	}
}
